package cluster

import (
	"godis/interface/resp"
	"godis/resp/reply"
)

// rename k1 k2
func rename(cluster *ClusterDatabase, c resp.Connection, cmdArgs [][]byte) resp.Reply {
	if len(cmdArgs) != 3 {
		return reply.MakeArgNumErrPeply("rename")
	}
	oldKey := string(cmdArgs[1])
	newKey := string(cmdArgs[2])
	oldNode := cluster.peerPicker.PickNode(oldKey)
	newNode := cluster.peerPicker.PickNode(newKey)
	// todo 删除oldNode中的oldKey 在newNode中新建一个newKey:oldValue
	if oldNode != newNode {
		return reply.MakeErrReply("ERR rename must within the same peer")
	}
	return cluster.relay(newNode, c, cmdArgs)
}
